import * as bannerModel from '../../models/banner';
import * as bannerRelationModel from '../../models/banner_relation';
import * as guideModel from '../../models/guide';
import * as feedbackModel from '../../models/feedback';
import * as VersionRobotModel from '../../models/version_robot';
import * as VersionAppModel from '../../models/version_app';
import { ValidException, EmptyException, SystemException } from '../../helps/exception';

export function* listBanner({ page, number, order, direction }) {
  return yield bannerModel.list(page, number, order, direction);
}

export function* addBanner({ data }) {
  const banner = yield bannerModel.add(data);
  if (!banner) throw SystemException('操作失败');
  return { id: banner.id };
}

export function* getBanner({ banner_id }) {
  if (!banner_id) throw ValidException('id 不能为空');
  const banner = yield bannerModel.get(banner_id);
  if (!banner) throw EmptyException('无相关信息');
  return banner;
}

export function* putBanner({ banner_id, data }) {
  if (!banner_id) throw ValidException('id 不能为空');
  const b = yield bannerModel.put(banner_id, data);
  if (!b) throw SystemException('操作失败');
  return { id: banner_id };
}

export function* delBanner({ banner_id }) {
  if (!banner_id) throw ValidException('id 不能为空');
  const b = yield bannerModel.del(banner_id);
  if (!b) throw SystemException('操作失败');
  return { id: banner_id };
}

export function* listBannerRelation({ banner_id, page, number, order, direction }) {
  if (!banner_id) throw ValidException('id 不能为空');
  return yield bannerRelationModel.listByBanner(banner_id, page, number, order, direction);
}

export function* addBannerRelation({ banner_id, module, module_id, order }) {
  if (!banner_id) throw ValidException('id 不能为空');
  const banner = yield bannerRelationModel.add({ banner_id, module, module_id, order });
  if (!banner) throw SystemException('操作失败');
  return { id: banner.id };
}

export function* delBannerRelation({ banner_id, relation_id }) {
  if (!banner_id || !relation_id) throw ValidException('id 不能为空');
  const b = yield bannerRelationModel.del(relation_id);
  if (!b) throw SystemException('操作失败');
  return { id: relation_id };
}

export function* listGuide({ page, number, order, direction }) {
  return yield guideModel.list(page, number, order, direction);
}

export function* addGuide({ data }) {
  const guide = yield guideModel.add(data);
  if (!guide) throw SystemException('操作失败');
  return { id: guide.id };
}

export function* getGuide({ guide_id }) {
  if (!guide_id) throw ValidException('id 不能为空');
  const guide = yield guideModel.get(guide_id);
  if (!guide) throw EmptyException('无相关信息');
  return guide;
}

export function* putGuide({ guide_id, data }) {
  if (!guide_id) throw ValidException('id 不能为空');
  const b = yield guideModel.put(guide_id, data);
  if (!b) throw SystemException('操作失败');
  return { id: guide_id };
}

export function* delGuide({ guide_id }) {
  if (!guide_id) throw ValidException('id 不能为空');
  const b = yield guideModel.del(guide_id);
  if (!b) throw SystemException('操作失败');
  return { id: guide_id };
}

export function* listFeedback({ page, number, order, direction }) {
  return yield feedbackModel.list(page, number, order, direction);
}

export function* getFeedback({ feedback_id }) {
  if (!feedback_id) throw ValidException('id 不能为空');
  const feedback = yield bannerModel.get(feedback_id);
  if (!feedback) throw EmptyException('无相关信息');
  return feedback;
}

export function* delFeedback({ feedback_id }) {
  if (!feedback_id) throw ValidException('id 不能为空');
  const b = yield feedbackModel.del(feedback_id);
  if (!b) throw SystemException('操作失败');
  return { id: feedback_id };
}

export function* getVersionRobot() {
  let version = yield VersionRobotModel.getLast();
  if (!version) version = {};
  return version;
}

export function* putVersionRobot({ data }) {
  const version = yield VersionRobotModel.add(data);
  if (!version) throw SystemException('操作失败');
  return { id: version.id };
}

export function* getVersionApp() {
  let version = yield VersionAppModel.getLast();
  if (!version) version = {};
  return version;
}

export function* putVersionApp({ data }) {
  const version = yield VersionAppModel.add(data);
  if (!version) throw SystemException('操作失败');
  return { id: version.id };
}
